/**
 * Connexion
 * A connexion is the link into two ResearchCenter.
 * 
 * @param  firstPoint - ResearchCenter - The researchCenter where start the connexion.
 * @param  secondPoint - ResearchCenter - The researchCenter where finish the connexion.
 * @param  stage - createjs.Stage - The context.
 */
var Connexion = function(firstPoint, secondPoint, stage){
    
    var _shape;
    var _currentTranslations = new Array();
    var _firstPoint;
    var _secondPoint;
    var _addConnexionToPoint= function(point, connexion){
        if(point instanceof ResearchCenter){
            if(!point.getConnexion()){
                point.setConnexion(connexion);
            }
        } else if (point instanceof CommunicationNode){
            point.addConnexion(connexion);
        }
    };
    
    this.getOppositePointFrom = function(point){
        if(point.getX() == _firstPoint.getX()  && point.getY() == _firstPoint.getY())  return _secondPoint;
        if(point.getX() == _secondPoint.getX() && point.getY() == _secondPoint.getY()) return _firstPoint;
        return null;
    }
    
    this.getOppositePointFromXY = function(x, y){
        if(x == _firstPoint.getX()  && y == _firstPoint.getY())  return _secondPoint;
        if(x == _secondPoint.getX() && y == _secondPoint.getY()) return _firstPoint;
        return null;
    }
    
    _firstPoint = firstPoint
    _secondPoint = secondPoint;

    //Creating the shape
    _shape = new createjs.Shape();
    
    // Generate image.
    _image = _image = ImageEngine.getInstance().getPipe();

    // Generate the graphic element.
    // Calculer l'hypothénuse.
    var hypothenuse = Math.sqrt((Math.pow((secondPoint.getX() - firstPoint.getX()), 2)) + (Math.pow((secondPoint.getY() - firstPoint.getY()), 2)));
    
    // Calculer l'orientation du trait.
    var diffX = secondPoint.getX() - firstPoint.getX();
    var diffY = secondPoint.getY() - firstPoint.getY();

    // Calcul de l'angle.
    
    var angle = (Math.atan(diffY/diffX))*180/Math.PI;
    if(diffX < 0){
        // Inversion de la direction si le sens est inversé.
        angle = angle - 180;
    }

    _shape.graphics.beginBitmapFill(_image).drawRect(0, 0, hypothenuse, _image.height);
    // Center the element.
    _shape.x = firstPoint.getX();
    _shape.y = firstPoint.getY();
    
    _shape.regY = _image.height/2;
    
    _shape.rotation = angle;
    //_shape.graphics.beginStroke("green").moveTo(firstPoint.getX(), firstPoint.getY()).lineTo(secondPoint.getX(), secondPoint.getY());
    
    _addConnexionToPoint(firstPoint, this);
    _addConnexionToPoint(secondPoint, this);
    
    stage.addChildAt(_shape, 0);
    
    this.getFirstPoint = function(){
        return _firstPoint
    };
    this.getSecondPoint = function(){
        return _secondPoint
    };
    
    this.addTranslation = function(translation){
        _currentTranslations.push(translation);
    }
    
    this.getCurrentTranslations = function(){
        return _currentTranslations;
    }
    
    this.processTranslations = function(timeElapsedInMS){
        //TODO
        //si le paquet est toujours sur la ligne, on le déplace
        for (var translationIndex in _currentTranslations){
            
            var packet = _currentTranslations[translationIndex].getPacket();
            var translation = _currentTranslations[translationIndex];
            
            var distanceCoef = (timeElapsedInMS/1000)*packet.getSpeed();
            
            var destination = translation.getDestinationNode();

            var diffX = destination.getX()-packet.getX();
            var diffY = destination.getY()-packet.getY();
            
            var angle = Math.atan(diffX/diffY);
            //console.log(angle)
            var movX = (Math.sin(angle)*distanceCoef);
            var movY = (Math.cos(angle)*distanceCoef);
            
            if(diffY<0){
                movX*=-1;
                movY*=-1;
            }
            
            var newX = movX+packet.getX();
            var newY = movY+packet.getY();
            
            //if the packet has ended his travel
            if(
            (newX>packet.getX() && newX >= destination.getX()) ||
            (newY>packet.getY() && newY >= destination.getY()) ||
            (newX<packet.getX() && newX <= destination.getX()) ||
            (newY<packet.getY() && newY <= destination.getY())
                ){
                //removing from array
                _currentTranslations.splice(translationIndex, 1);
                //queuing packet to the next comNode
                destination.addPacketToWaitingList(packet);
                //TODO
            } else {
                //moving the packet
                packet.setX(newX);
                packet.setY(newY);
            }
            
            
            
        }
    //puis, si le paquet est hors de la ligne (ou exactement à son extremité, on l'ajoute à la file d'attente du noeud et on le supprime
    };
}